﻿namespace Nasty.Core.Model
{
    public class ExcelItemModel
    {
        /// <summary>
        /// 字段名称
        /// </summary>
        public required string Field { get; set; }

        /// <summary>
        /// Excel列名称
        /// </summary>
        public required string Name { get; set; }

        /// <summary>
        /// 自定义格式化
        /// </summary>

        public Func<dynamic, string> Format { get; set; } = ExcelItemFunc.DefaultFormat;
    }


    /// <summary>
    /// Excel导出时的格式化函数，很多未测试
    /// </summary>
    public static class ExcelItemFunc
    {
        public static Func<dynamic, string> DefaultFormat = (value) => value;

        public static Func<dynamic, string> DateTimeFormat = (value) =>
        {
            if (value is DateTime dateTime)
            {
                return dateTime.ToString("yyyy-MM-dd HH:mm:ss");
            }
            return value.ToString();
        };

        public static Func<dynamic, string> DateFormat = (value) =>
        {
            if (value is DateTime dateTime)
            {
                return dateTime.ToString("yyyy-MM-dd");
            }
            return value.ToString();
        };

        public static Func<dynamic, string> TimeFormat = (value) =>
        {
            if (value is DateTime dateTime)
            {
                return dateTime.ToString("HH:mm:ss");
            }
            return value.ToString();
        };

        public static Func<dynamic, string> NumberFormat = (value) =>
        {
            if (value is decimal decimalValue)
            {
                return decimalValue.ToString("0.00");
            }
            return value.ToString();
        };

        public static Func<dynamic, string> CurrencyFormat = (value) =>
        {
            if (value is decimal decimalValue)
            {
                return decimalValue.ToString("C");
            }
            return value.ToString();
        };

        public static Func<dynamic, string> PercentageFormat = (value) =>
        {
            if (value is decimal decimalValue)
            {
                return decimalValue.ToString("P");
            }
            return value.ToString();
        };

        public static Func<dynamic, string> PhoneFormat = (value) =>
        {
            if (value is string strValue && strValue.Length == 11)
            {
                return $"{strValue.Substring(0, 3)}****{strValue.Substring(7)}";
            }
            return value.ToString();
        };

        public static Func<dynamic, string> EmailFormat = (value) =>
        {
            if (value is string strValue && strValue.Contains("@"))
            {
                var parts = strValue.Split('@');
                if (parts.Length == 2)
                {
                    return $"{parts[0].Substring(0, 2)}****@{parts[1]}";
                }
            }
            return value.ToString();
        };

        public static Func<dynamic, string> IDCardFormat = (value) =>
        {
            if (value is string strValue && strValue.Length == 18)
            {
                return $"{strValue.Substring(0, 6)}****{strValue.Substring(14)}";
            }
            return value.ToString();
        };

        public static Func<dynamic, string> AddressFormat = (value) =>
        {
            if (value is string strValue)
            {
                return strValue.Length > 10 ? $"{strValue.Substring(0, 10)}..." : strValue;
            }
            return value.ToString();
        };

        public static Func<dynamic, string> BoolFormat = (value) =>
        {
            if (value is bool vb)
            {
                return vb ? "是" : "否";
            }
            return value.ToString();
        };

        public static Func<dynamic, string> EnumFormat = (value) =>
        {
            if (value is Enum enumValue)
            {
                return enumValue.ToString();
            }
            return value.ToString();
        };

        public static Func<dynamic, string> ImageFormat = (value) =>
        {
            if (value is string strValue && strValue.StartsWith("http"))
            {
                return $"![Image]({strValue})";
            }
            return value.ToString();
        };

        public static Func<dynamic, string> FileFormat = (value) =>
        {
            if (value is string strValue && strValue.StartsWith("http"))
            {
                return $"[File]({strValue})";
            }
            return value.ToString();
        };

        public static Func<dynamic, string> LinkFormat = (value) =>
        {
            if (value is string strValue && strValue.StartsWith("http"))
            {
                return $"[Link]({strValue})";
            }
            return value.ToString();
        };

        public static Func<dynamic, string> QRCodeFormat = (value) =>
        {
            if (value is string strValue && strValue.StartsWith("http"))
            {
                return $"[QR Code]({strValue})";
            }
            return value.ToString();
        };

        public static Func<dynamic, string> ColorFormat = (value) =>
        {
            if (value is string strValue && strValue.StartsWith("#"))
            {
                return $"<span style='color:{strValue}'>{strValue}</span>";
            }
            return value.ToString();
        };

        public static Func<dynamic, string> MarkdownFormat = (value) =>
        {
            if (value is string strValue)
            {
                return $"```markdown\n{strValue}\n```";
            }
            return value.ToString();
        };

        public static Func<dynamic, string> HTMLFormat = (value) =>
        {
            if (value is string strValue)
            {
                return $"<div>{strValue}</div>";
            }
            return value.ToString();
        };

        public static Func<dynamic, string> JSONFormat = (value) =>
        {
            if (value is string strValue)
            {
                return $"```json\n{strValue}\n```";
            }
            return value.ToString();
        };

        public static Func<dynamic, string> XMLFormat = (value) =>
        {
            if (value is string strValue)
            {
                return $"```xml\n{strValue}\n```";
            }
            return value.ToString();
        };

        public static Func<dynamic, string> CSVFormat = (value) =>
        {
            if (value is string strValue)
            {
                return $"```csv\n{strValue}\n```";
            }
            return value.ToString();
        };


    }
}
